<template>
  <el-dialog
    :before-close="handleClose"
    title="编辑信息"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="forwarder"
      v-loading="loading"
      label-width="100px"
      :model="forwarder"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="信息" prop="forwarder_info">
        <el-input
          v-model="forwarder.forwarder_info"
          placeholder="请输入内容"
          type="textarea"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('forwarder')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { editInfo } from "@/api/setting/forwarder.js";
import clearWatch from "@/utils/mixin/clearWatch";
export default {
  name: "EditForwarderName",
  mixins: [clearWatch],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      forwarder: {
        forwarder_id: "",
        forwarder_info: ""
      },
      rules: {
        forwarder_info: [
          { required: true, message: "请输入名称", trigger: "blur" }
        ]
      },
      forwarder_id: ""
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    show(info, id) {
      this.dialogVisible = !this.dialogVisible;
      this.forwarder.forwarder_info = JSON.stringify(info, null, "\t");
      this.forwarder.forwarder_id = id;
    },

    submit(formName) {
      console.log(formName);
      this.$refs[formName].validate(valid => {
        if (valid) {
          editInfo({
            forwarder_id: this.forwarder.forwarder_id,
            forwarder_info: JSON.parse(this.forwarder.forwarder_info)
          }).then(res => {
            this.$message.success(res.data.message);
            this.dialogVisible = false;
            this.$emit("change");
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
